import {PixelRatio} from 'react-native'

export const isMobile = (number) => {
  // let reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
  // return reg.test(number);
  return number.length === 11
};

export const isID = (id) => {
  return id.length === 18;
  // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X
  // let reg = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/;
  // return reg.test(id);
};
export const ToString = (value) => {
  if (value === null) {
    return '';
  } else if (value === undefined) {
    return '';
  } else {
    return value + '';
  }
};
export const sexInId = (id) => {
  let sex = id.substring(17, 1);
  return (sex % 2 === 0 ? 2 : 1);
};

export const sexTextInId = (id) => {
  let sex = id.substring(17, 1);
  return (sex % 2 === 0 ? '女' : '男');
};
export const hasString = (string) => {

  if (string === null) {
    return false;
  } else if (string === undefined) {
    return false;
  }
  // else return (string.length !== 0);
  else return string.length !== 0;
};

export const noNullString = (text) => hasString(text) ? text : '';
export const timeText = (dateStmp = null) => {
  let date = dateStmp ? new Date(dateStmp) : new Date();
  let hour = date.getHours() > 9 ? date.getHours() : '0' + date.getHours();
  let minute = date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes();
  // Modify by Pegasus : 修改小时与分钟的显示，不足两位的，前面加0
  // return date.getFullYear()+ '年' + (date.getMonth()+1) + '月' + date.getDate() + '日 ' + date.getHours() + ':' + date.getMinutes();
  return date.getFullYear() + '年' + (date.getMonth() + 1) + '月' + date.getDate() + '日 ' + hour + ':' + minute;
};

// 日期显示
export const miniTimeText = (dateStmp = null) => {
  let date = dateStmp ? new Date(dateStmp) : new Date();
  // return date.getFullYear() + '年' + (date.getMonth() + 1) + '月' + date.getDate() + '日';
  return date.getFullYear() + '年' + (date.getMonth() + 1) + '月';
};

export const miniTimeTextWithSplite = (dateStmp = null) => {
  let date = dateStmp ? new Date(dateStmp) : new Date();
  return date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate();
};

export const kilometreText = (value = null) => {
  let moneyV = moneyValue(value);
  if (moneyV >= 1000) {
    return Number(moneyV / 10000).toFixed(2) + '万公里'
  } else if (isNaN(moneyV)) {
    return '-';
  } else {
    return parseInt(moneyV) + '公里'
  }
};

export const dateValue = (dateString) => {
  if (!hasString(dateString)) {
    return null;
  }
  let date = dateString.split('-');
  return {
    year: date[0],
    month: date[1],
    day: date[2],
  }
};
export const timestampToDate = (timestamp) => {
  let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
  let Y = date.getFullYear() + '-';
  let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  let D = date.getDate() + 1 + '';
  return Y + M + D;
};


export const dateParameter = (date: { year: String, month: String, day: string }) => {
  if (date === null || date === undefined) {
    return null;
  }
  return date.year + '-' + date.month + (date.day ? '-' + date.day : '');
};


export const dateText = (date: { year: String, month: String, day: string }) => {
  if (!hasString(date)) {
    return '-'
  }
  return date.year + '年' + date.month + '月' + (date.day ? date.day + '日' : '');
};

export const dateSimpleText = (date: { year: String, month: String, day: string }) => {
  if (!hasString(date)) {
    return '-'
  }
  return date.year + '/' + date.month + (date.day ? '/' + date.day : '');
};


export const moneyText = (value) => {
  let moneyV = moneyValue(value);
  if (moneyV >= 10000) {
    return Number(moneyV / 10000).toFixed(2) + '万元'
  } else if (isNaN(moneyV)) {
    return '-';
  } else {
    return parseInt(moneyV) + '元'
  }

};

export const moneyValue = (value) => {
  if (value === parseInt(value)) {
    return value;
  }
  if (value === parseFloat(value)) {
    return value;
  } else if (hasString(value) && value.type !== 'int' && value.indexOf('万') >= 0) {
    return parseInt(value) * 10000;
  } else {
    return parseInt(value);
  }
};


export const EncodeUtf8 = (s1) => {
  let s = escape(s1);
  let sa = s.split("%");
  let retV = "";
  if (sa[0] !== "") {
    retV = sa[0];
  }
  for (let i = 1; i < sa.length; i++) {
    if (sa[i].substring(0, 1) === "u") {
      retV += Hex2Utf8(Str2Hex(sa[i].substring(1, 5)));

    } else retV += "%" + sa[i];
  }

  return retV;
};

// 拼接车辆颜色
export const showCarColors = (colors) => {
  let colorString = '';

  if (hasString(colors)) {
    for (let i = 0; i < colors.length; i++) {
      if (colors[i].isExis) {
        colorString += getColor(colors[i].color) + ' | ';
      }
    }

    return colorString.substring(0, colorString.length - 2);
  } else {
    return null;
  }
};

function getColor(color) {
  if (hasString(color)) {
    let index = color.indexOf(',');
    return color.substring(0, index);
  } else {
    return ''
  }
}

/**
 * @return {string}
 */
function Str2Hex(s) {
  let c = "";
  let n;
  let ss = "0123456789ABCDEF";
  let digS = "";
  for (let i = 0; i < s.length; i++) {
    c = s.charAt(i);
    n = ss.indexOf(c);
    digS += Dec2Dig(eval(n));

  }
  //return value;
  return digS;
}

/**
 * @return {string}
 */
function Dec2Dig(n1) {
  let s = "";
  let n2 = 0;
  for (let i = 0; i < 4; i++) {
    n2 = Math.pow(2, 3 - i);
    if (n1 >= n2) {
      s += '1';
      n1 = n1 - n2;
    } else
      s += '0';

  }
  return s;

}

/**
 * @return {number}
 */
function Dig2Dec(s) {
  let retV = 0;
  if (s.length === 4) {
    for (let i = 0; i < 4; i++) {
      retV += eval(s.charAt(i)) * Math.pow(2, 3 - i);
    }
    return retV;
  }
  return -1;
}

/**
 * @return {string}
 */
function Hex2Utf8(s) {
  let retS = "";
  let tempS = "";
  let ss = "";
  if (s.length === 16) {
    tempS = "1110" + s.substring(0, 4);
    tempS += "10" + s.substring(4, 10);
    tempS += "10" + s.substring(10, 16);
    let sss = "0123456789ABCDEF";
    for (let i = 0; i < 3; i++) {
      retS += "%";
      ss = tempS.substring(i * 8, (eval(i) + 1) * 8);


      retS += sss.charAt(Dig2Dec(ss.substring(0, 4)));
      retS += sss.charAt(Dig2Dec(ss.substring(4, 8)));
    }
    return retS;
  }
  return "";
}

export function hasChinese(str) {
  const reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
  if (reg.test(str)) {
    return true;
  } else {
    return false;
  }
}

export function parseNumber(number, count) {
  if (Math.floor(number) === number) {
    return Number(number)
  } else {
    return Number(number).toFixed(count)
  }
}

export const hasValue = obj =>
  obj !== null && obj !== undefined && obj.length > 0 && obj !== '';

export const PX = (1.0 / PixelRatio.get());